package com.ivo.printer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.util.Random;

import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

public class Label implements Printable {

    private static final int INCH = 72;

    private double width;
    private double height;

    private Graphics2D g2d;

    public Label(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public void printLabel() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        try {
            printJob.setPrintService(printJob.getPrintService());
        } catch (PrinterException e) {
        }
        PageFormat pageFormat = printJob.defaultPage();
        Paper paper = pageFormat.getPaper();
        String jobName = "Label - " + System.currentTimeMillis() + " - " + new Random().nextInt(Integer.MAX_VALUE);
        printJob.setJobName(jobName);
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        double w = UnitUtil.mm2in(width) * INCH;
        double h = UnitUtil.mm2in(height) * INCH;
        paper.setSize(w, h);
        paper.setImageableArea(0, 0, w, h);
        pageFormat.setPaper(paper);
        Book book = new Book();
        book.append(this, pageFormat);
        printJob.setPageable(book);
        try {
            printJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex == 0) {
            g2d = (Graphics2D) graphics;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            AffineTransform at = new AffineTransform();
            at.translate(0, 0);
            double zoom = 1.0;
            at.scale(zoom, zoom);
            g2d.transform(at);
            double w = UnitUtil.mm2in(width) * INCH;
            double h = UnitUtil.mm2in(height) * INCH;
            Shape shape = g2d.getClip();
            g2d.clip(new Rectangle(0, 0, (int) w, (int) h));
            try {
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, (int) w, (int) h);
                g2d.setColor(Color.BLACK);
                g2d.setStroke(new BasicStroke(1));
                g2d.setFont(new Font("宋体", Font.ITALIC, 9));
                g2d.drawString("WG.TECH", 5, 10);
                g2d.setFont(new Font("宋体", Font.PLAIN, 7));
                g2d.drawString("沃格光电", 20, 20);
                g2d.setFont(new Font("宋体", Font.PLAIN, 9));
                g2d.drawString("BOX ID", 5, 36);
                g2d.drawString("箱号", 5, 50);
                g2d.drawString("江西沃格光电原料标签", 60, 20);
                g2d.drawRect(168, 12, 25, 10);
                g2d.drawString("ROHS", 170, 20);

                Code128Bean bean = new Code128Bean();
                bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
                bean.doQuietZone(false);
                bean.setHeight(9);
                bean.setModuleWidth(0.3);
                BitmapCanvasProvider canvas = new BitmapCanvasProvider(INCH, BufferedImage.TYPE_BYTE_BINARY, true, 0);
                bean.generateBarcode(canvas, "wfq12345");
                try {
                    canvas.finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BufferedImage bi = canvas.getBufferedImage();
                g2d.drawImage(bi, 40, 28, bi.getWidth(), bi.getHeight(), Color.WHITE, null);

                g2d.setFont(new Font("Arial", Font.PLAIN, 8));
                g2d.drawString("wfq12345", 70, 62);

                g2d.drawRect(5, 65, 190, 75);

                g2d.drawLine(5, 80, 195, 80);
                g2d.drawLine(5, 95, 195, 95);
                g2d.drawLine(5, 110, 195, 110);
                g2d.drawLine(5, 125, 195, 125);

                g2d.drawLine(40, 65, 40, 140);

                g2d.drawLine(120, 65, 120, 140);

                g2d.drawLine(80, 65, 80, 80);

                g2d.drawLine(80, 95, 80, 140);

                g2d.drawLine(140, 80, 140, 95);
            } finally {
                g2d.setClip(shape);
            }
            return Printable.PAGE_EXISTS;
        } else {
            return Printable.NO_SUCH_PAGE;
        }
    }

    public static void main(String[] args) {
        Label label = new Label(70, 50);
        label.printLabel();
    }
}
